package controller

import (
	"context"
	v1 "wh-1-backend/api/v1"
	"wh-1-backend/internal/model"
	"wh-1-backend/internal/model/do"
	"wh-1-backend/internal/service"
)

type cDictionary struct {
}

var Dictionary = cDictionary{}

func (d *cDictionary) TypeList(ctx context.Context, req *v1.DictionaryTypeListReq) (res *v1.DictionaryTypeRes, err error) {
	list, err := service.Dictionary().List(ctx)
	if err == nil {
		res = &v1.DictionaryTypeRes{
			List: list,
		}
	}
	return
}
func (d *cDictionary) CreateType(ctx context.Context, req *v1.CreateDicTypeReq) (res *v1.Res, err error) {
	err = service.Dictionary().SaveDicType(ctx, &do.Dictionary{
		Value: req.Value,
		Code:  req.Code,
	})
	return
}

func (d *cDictionary) DelDicType(ctx context.Context, req *v1.DelDicTypeReq) (res *v1.Res, err error) {
	err = service.Dictionary().DelDicType(ctx, req.Id)
	return
}

func (d *cDictionary) ItemList(ctx context.Context, req *v1.DictionaryItemListReq) (res *v1.DictionaryItemListRes, err error) {
	total, list, err := service.Dictionary().ItemPageList(ctx, req.Type, req.Page, req.PageSize)
	if err == nil {
		res = &v1.DictionaryItemListRes{
			List:  list,
			Total: total,
		}
	}
	return
}

func (d *cDictionary) CreateDic(ctx context.Context, req *v1.CreateDicReq) (res *v1.Res, err error) {
	err = service.Dictionary().SaveDicItem(ctx, model.CreateDicInput{
		Type:  req.Type,
		Code:  req.Code,
		Value: req.Value,
	})
	return
}

func (d *cDictionary) ModifyDic(ctx context.Context, req *v1.ModifyDicReq) (res *v1.Res, err error) {
	err = service.Dictionary().UpdateDicItem(ctx, req.Id, model.CreateDicInput{Value: req.Value, Code: req.Code})
	return
}

func (d *cDictionary) DeleteDic(ctx context.Context, req *v1.DeleteDicReq) (res *v1.Res, err error) {
	err = service.Dictionary().DeleteDicItem(ctx, req.Id)
	return
}

func (d *cDictionary) List(ctx context.Context, req *v1.DictionaryListReq) (res *v1.DictionaryListRes, err error) {
	list, err := service.Dictionary().ItemList(ctx, req.Type)
	res = &v1.DictionaryListRes{List: list}
	return
}
